import { createSlice, createAsyncThunk, PayloadAction } from '@reduxjs/toolkit';
import { KnowledgeBase } from '../../types/knowledgeBase';
import { knowledgeBaseApi } from '../../api/knowledgeBase';

// 状态类型
interface KnowledgeBaseState {
  knowledgeBases: KnowledgeBase[];
  currentKnowledgeBase: KnowledgeBase | null;
  loading: boolean;
  error: string | null;
}

// 初始状态
const initialState: KnowledgeBaseState = {
  knowledgeBases: [],
  currentKnowledgeBase: null,
  loading: false,
  error: null,
};

// 异步Thunk - 获取所有知识库
export const fetchKnowledgeBases = createAsyncThunk(
  'knowledgeBase/fetchAll',
  async (_, { rejectWithValue }) => {
    try {
      return await knowledgeBaseApi.getAll();
    } catch (error) {
      return rejectWithValue('获取知识库列表失败');
    }
  }
);

// 异步Thunk - 获取单个知识库
export const fetchKnowledgeBaseById = createAsyncThunk(
  'knowledgeBase/fetchById',
  async (id: number, { rejectWithValue }) => {
    try {
      return await knowledgeBaseApi.getById(id);
    } catch (error) {
      return rejectWithValue('获取知识库详情失败');
    }
  }
);

// 异步Thunk - 创建知识库
export const createKnowledgeBase = createAsyncThunk(
  'knowledgeBase/create',
  async (data: Omit<KnowledgeBase, 'id'>, { rejectWithValue }) => {
    try {
      return await knowledgeBaseApi.create(data);
    } catch (error) {
      return rejectWithValue('创建知识库失败');
    }
  }
);

// 知识库状态切片
const knowledgeBaseSlice = createSlice({
  name: 'knowledgeBase',
  initialState,
  reducers: {
    clearCurrentKnowledgeBase: (state) => {
      state.currentKnowledgeBase = null;
    },
    clearError: (state) => {
      state.error = null;
    },
  },
  extraReducers: (builder) => {
    // 获取所有知识库
    builder
      .addCase(fetchKnowledgeBases.pending, (state) => {
        state.loading = true;
        state.error = null;
      })
      .addCase(fetchKnowledgeBases.fulfilled, (state, action: PayloadAction<KnowledgeBase[]>) => {
        state.loading = false;
        state.knowledgeBases = action.payload;
      })
      .addCase(fetchKnowledgeBases.rejected, (state, action) => {
        state.loading = false;
        state.error = action.payload as string;
      });

    // 获取单个知识库
    builder
      .addCase(fetchKnowledgeBaseById.pending, (state) => {
        state.loading = true;
        state.error = null;
      })
      .addCase(fetchKnowledgeBaseById.fulfilled, (state, action: PayloadAction<KnowledgeBase>) => {
        state.loading = false;
        state.currentKnowledgeBase = action.payload;
      })
      .addCase(fetchKnowledgeBaseById.rejected, (state, action) => {
        state.loading = false;
        state.error = action.payload as string;
      });

    // 创建知识库
    builder
      .addCase(createKnowledgeBase.pending, (state) => {
        state.loading = true;
        state.error = null;
      })
      .addCase(createKnowledgeBase.fulfilled, (state, action: PayloadAction<KnowledgeBase>) => {
        state.loading = false;
        state.knowledgeBases.push(action.payload);
        state.currentKnowledgeBase = action.payload;
      })
      .addCase(createKnowledgeBase.rejected, (state, action) => {
        state.loading = false;
        state.error = action.payload as string;
      });
  },
});

export const { clearCurrentKnowledgeBase, clearError } = knowledgeBaseSlice.actions;
export default knowledgeBaseSlice.reducer; 